'''
Created on 09-07-10

@author: peterd
'''




from web.service.LBCParser import LBCParser
from geo.models import NoSuchCity, NoSuchStreet, NoSuchLocation
from infra.logging import logger


class LubaParser(LBCParser):
	'''
	classdocs
	'''

	def __init__(self, lbcategory, dataProcessor): 
		LBCParser.__init__(self, lbcategory, dataProcessor)

		self.inAtmTable_ = False
		self.fieldIdx_ = 0
				
		log = logger.getLogger()
		log.debug("%s = %s" % (self, self.__dict__))
		
		
	def reset__(self):
		self.fieldIdx_ = 0
		
		LBCParser.reset__(self)


	def start_table(self, attrs):
		for attr, value in attrs:
			if attr == 'class' and value == 'suvaha':
				self.inAtmTable_ = True


	def end_table(self):
		if self.inAtmTable_:
			self.inAtmTable_ = False


	def start_td(self, attrs):
		if self.inAtmTable_:
			self.fieldIdx_ += 1

			
	def do_handle_data(self, data):
		log = logger.getLogger()
									
		if self.inAtmTable_:
			if self.fieldIdx_ == 1:
				try:
					self.setCity(data)
				except NoSuchCity, e:
					log.error('NoSuchCity exception caught, reason: %s' % e)
					
			if self.fieldIdx_ == 2:
				try:
					self.setStreet(data)
				except NoSuchStreet, e:
					log.error('NoSuchStreet exception caught, reason: %s' % e)

			if self.fieldIdx_ == 3:
				try:
					if len(self.errors_) == 0:
						self.building_ = data
						self.createOrganization(name = 'Volksbank Slovensko, a.s.', \
								            	url = 'http://www.volksbank.sk', \
								            	phone = 'Call Centrum: 0850 123 123', \
												logo = 'icons/lbc/atm/lubabanka.png', \
												email = '')
				except NoSuchLocation, e:
					log.error('NoSuchStreet exception caught, reason: %s' % e)

				self.reset__()

						
	def getId(self):
		return self.__class__.__name__